package com.gogowan.agroenterprise.contorl;

import java.lang.Thread.UncaughtExceptionHandler;

import com.gogowan.agroenterprise.ActivityCollector;
import com.gogowan.agroenterprise.utils.MyLog;

import android.content.Context;
import android.util.Log;

/**
 * 程序异常处理
 * @author zk
 *
 */
public class AppExcepiton implements UncaughtExceptionHandler {

	// 获取application 对象�?
	private Context mContext;

	private Thread.UncaughtExceptionHandler defaultExceptionHandler;
	// 单例声明CustomException;
	private static AppExcepiton appException;

	private AppExcepiton() {
	}

	public static AppExcepiton getInstance() {
		if (appException == null) {
			appException = new AppExcepiton();
		}
		return appException;
	}
    /**
     * 在thread里面有次方法
     */
	@Override
	public void uncaughtException(Thread thread, Throwable exception) {
		String path = null;
		StackTraceElement[] stack = exception.getCause().getStackTrace();  
		
//			//输出到日�?
//			MyLog myLog=new MyLog(mContext,"system.log",stack);
//			myLog.writeSDCardLog("SuperPai","["+exception.getLocalizedMessage()+"]");
//			
//			Log.e("tag", "exception >>>>>>>" + exception.getLocalizedMessage());
//			// 将异常抛出，则应用会弹出异常对话�?
//			defaultExceptionHandler.uncaughtException(thread, exception);
			ActivityCollector.finishAll();
			android.os.Process.killProcess(android.os.Process.myPid());

		
	}
	
	/**
	 * 初始�?
	 * @param context
	 */
	public void init(Context context) {
		mContext = context;
		defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(this);
	}
//	/**
//	 * 获得当前时间
//	 * @return
//	 */
//	public String getCurrentTime(){
//		Time t = new Time();
//		t.setToNow();
//		int year = t.year;
//		int month = t.month+1;
//		int day =  t.monthDay;
//		int hour = t.hour;
//		int minute = t.minute;
//		int second =  t.second;
//		String time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
//		return time;
//		
//	}
}